# -*- coding: utf-8 -*-
# __init__.py create by kumiler
# on 2022/8/28 15:16
# desc

from datetime import timedelta, date
import datetime
from utils.operators.rich_sql_sensor import RichSqlSensor
# from jms.dm.dm_waybill_customer_complaint.complaint_history.daily import jms_dm__dm_waybill_customer_complaint_daily_dt
from time import time
# from jms.dm.dm_waybill_customer_complaint.complaint_history.month import jms_dm__dm_waybill_customer_complaint_month_dt

daily = date.today() - timedelta(days=1)
lastday_lastmonth = date(daily.year, daily.month, 1) - datetime.timedelta(1)
doris_p = str(lastday_lastmonth).replace('-', '')

# 历史日明细导入
label_detail = f"dwd_waybill_customer_complaint_detail_new_dt_his_{int(time())}"
timeout = timedelta(minutes=30).seconds
doris_jms_dwd__dwd_waybill_customer_complaint_detail_dt_his = RichSqlSensor(
    task_id='doris_jms_dwd__dwd_waybill_customer_complaint_detail_dt_his',
    pool='broker_load_pool',
    email=['shenjiaming@jtexpress.com', 'yl_bigdata@yl-scm.com'],
    conn_id='new_doris',
    pre_sql=f"""
             TRUNCATE TABLE jms_dwd.dwd_waybill_customer_complaint_detail_new_dt PARTITION (p{doris_p});
             LOAD LABEL jms_dwd.{label_detail} (
                 DATA INFILE( "hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_waybill_customer_complaint_detail_new_dt/dt={lastday_lastmonth}/*")
                 INTO TABLE dwd_waybill_customer_complaint_detail_new_dt
                 FORMAT AS 'PARQUET'
             )
             WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
             PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dwd WHERE LABEL = '{label_detail}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dwd WHERE LABEL = '{label_detail}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

# doris_jms_dwd__dwd_waybill_customer_complaint_detail_dt_his << jms_dm__dm_waybill_customer_complaint_daily_dt

# 日聚合数据导入
label_dt = f"dws_waybill_customer_complaint_agg_new_dt_hisagg_{int(time())}"
doris_jms_dws__dws_waybill_customer_complaint_agg_daily_dt = RichSqlSensor(
    task_id='doris_jms_dws__dws_waybill_customer_complaint_agg_daily_dt',
    pool='broker_load_pool',
    email=['shenjiaming@jtexpress.com', 'yl_bigdata@yl-scm.com'],
    conn_id='new_doris',
    pre_sql=f"""
             TRUNCATE TABLE jms_dws.dws_waybill_customer_complaint_agg_new_dt PARTITION (p{doris_p});
             LOAD LABEL jms_dws.{label_dt} (
                 DATA INFILE("hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_waybill_customer_complaint_agg_new_dt/dt={lastday_lastmonth}/*")
                 INTO TABLE dws_waybill_customer_complaint_agg_new_dt
                 FORMAT AS 'PARQUET'
             )
             WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
             PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dws WHERE LABEL = '{label_dt}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dws WHERE LABEL = '{label_dt}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

# doris_jms_dws__dws_waybill_customer_complaint_agg_daily_dt << jms_dm__dm_waybill_customer_complaint_daily_dt

# 历史月明细导入


# doris表名
doris_table = "dwd_waybill_customer_complaint_detail_dt"
# label名
label_detail = f"{doris_table}_hisde_{int(time())}"
timeout = timedelta(minutes=30).seconds

doris_jms_dwd__dwd_waybill_customer_complaint_detail_month_dt = RichSqlSensor(
    task_id='doris_jms_dwd__dwd_waybill_customer_complaint_detail_month_dt',
    pool='broker_load_pool',
    email=['shenjiaming@jtexpress.com', 'yl_bigdata@yl-scm.com'],
    conn_id='new_doris',
    pre_sql=f"""
             TRUNCATE TABLE jms_dwd.dwd_waybill_customer_complaint_detail_dt PARTITION (p{doris_p});
             LOAD LABEL jms_dwd.{label_detail} (
                 DATA INFILE(
"hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_waybill_customer_complaint_detail_dt/dt={lastday_lastmonth}/*"
)
                 INTO TABLE {doris_table}
                 FORMAT AS 'PARQUET'
             )
             WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
             PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dwd WHERE LABEL = '{label_detail}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dwd WHERE LABEL = '{label_detail}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

# doris_jms_dwd__dwd_waybill_customer_complaint_detail_month_dt << jms_dm__dm_waybill_customer_complaint_month_dt

label_mt = f"dws_waybill_customer_complaint_agg_mt_hismagg_{int(time())}"
doris_jms_dws__dws_waybill_customer_complaint_agg_month_mt = RichSqlSensor(
    task_id='doris_jms_dws__dws_waybill_customer_complaint_agg_month_mt',
    pool='broker_load_pool',
    email=['shenjiaming@jtexpress.com', 'yl_bigdata@yl-scm.com'],
    conn_id='new_doris',
    pre_sql=f"""
             TRUNCATE TABLE jms_dws.dws_waybill_customer_complaint_agg_mt PARTITION (p{doris_p});
             LOAD LABEL jms_dws.{label_mt} (
                 DATA INFILE("hdfs://{{{{ var.value.hadoop_namespace }}}}/dw/hive/jms_dm.db/external/dm_waybill_customer_complaint_agg_mt/dt={lastday_lastmonth}/*")
                 INTO TABLE dws_waybill_customer_complaint_agg_mt
                 FORMAT AS 'PARQUET'
             )
             WITH BROKER '{{{{ var.json.doris_brokers | random_choice }}}}'
             PROPERTIES ('timeout'='{timeout}', 'max_filter_ratio'='0.0')""",
    poke_sql=f"SHOW LOAD FROM jms_dws WHERE LABEL = '{label_mt}' ORDER BY CreateTime DESC LIMIT 1",
    sql_on_kill=f"CANCEL LOAD FROM jms_dws WHERE LABEL = '{label_mt}'",
    success=lambda r: r[2] == 'FINISHED',
    failure=lambda r: (r[2] is not None and r[2] == 'CANCELLED', str(r[7])),
    poke_interval=60,
    execution_timeout=timedelta(seconds=timeout + 120), )

# doris_jms_dws__dws_waybill_customer_complaint_agg_month_mt << jms_dm__dm_waybill_customer_complaint_month_dt
